<?php
 //============================================================================
 // ____________________________PLIGG TEAM ADDON_______________________________
 //
// This CLASS was created by AnAlienHolakres3 ||| XXXXX  XX     XX XXXX   XXXX
// (c) 2007-2008                Jakub Holasek ||| XXXXX  XX     XX X      X
// @last_update                    21/02/2008 ||| XX     XX     XX X  XX  X  XX
// @version                              0.69 ||| XX     XXXXXX XX XXXXX  XXXXX
//
// This file can be used by Pligg community.
//=============================================================================

// Class TaskComment contains general information about task rating. 
// Each of tasks can be rated only by team members. Every member
// can rate only one times, next member´s rating just replace
// original value  

class TaskRating {
 
    
    private $task_id=0; 
    private $team_id=0;
    private $user_id=0;
    private $taskrating_value=0;
    
    private $taskrating_users;  
    private $taskrating_avg;
    private $read = true;
   
    
    //============================================================================
    //  Function read_taskrating() gets taskrating data (array) from database    
    //============================================================================      
    
    function read_taskrating() {
        global $db;
        
       $team_id = $this->team_id;     

      
      //get taskrating of all tasks in team (in future do it for filter also!)  

      $tu = $db->get_results("SELECT task_id,user_login,taskrating_value FROM " . table_taskrating. " JOIN " .table_users. " USING (user_id)
                           WHERE team_id=$team_id ORDER BY taskrating_value DESC"); 
                                           
      $ta = $db->get_results("SELECT task_id,avg(taskrating_value) as average_rating FROM " . table_taskrating. "      
                     WHERE team_id=$team_id GROUP BY task_id");
      
      
                                      
          if ($tu && $ta) {   
              $this->taskrating_users = $tu;
              $this->taskrating_avg = $ta;
              
               return true;
               $this->read = true;    
           }
              
              $this->read = false;
              return false;
    }
    
  
    
    //============================================================================
    //  Function fill_print() assign team taskrating smartys
    //============================================================================
      
    function fill_print()
{
    global $main_smarty;
    
    $taskrating_users =$this->taskrating_users;
    $taskrating_avg=$this->taskrating_avg;
    
    $task = new Task;
    $task->set_task_team_id($this->team_id);
    $task->read_task_ids();
    $task_ids = $task->get_tasks_ids();
    
    // no task = no taskrating
    if ($task_ids) {             
        
        if ($taskrating_users && $taskrating_avg) {     
            
            foreach($taskrating_users as $rating_users) {
                
                $taskrating_user_name[] = $rating_users->user_login;
                $taskrating_task_id_user[] = $rating_users->task_id;
                $taskrating_user_rating_value[] = $rating_users->taskrating_value;
            }
            foreach($taskrating_avg as $rating) {
                
                $taskrating_average[] = round($rating->average_rating,1);
                $taskrating_task_id[] = $rating->task_id;
                
            }
            
        }
        if (!$taskrating_task_id){
            $taskrating_task_id=Array();
        }
                                    
            foreach($task_ids as $task_id) {
                        
                if (!in_array($task_id->task_id,$taskrating_task_id)) {
                    $taskrating_task_id[]=$task_id->task_id;
                    $taskrating_average[]=0;
                }
            }
        
               
            $main_smarty->assign('taskrating_user_name', $taskrating_user_name);
            $main_smarty->assign('taskrating_user_task_id', $taskrating_task_id_user);
            $main_smarty->assign('taskrating_user_rating_value', $taskrating_user_rating_value);
            
            $main_smarty->assign('taskrating_average', $taskrating_average);
            $main_smarty->assign('taskrating_task_id', $taskrating_task_id);
        }
        
    
}
    //============================================================================
    //  Function remove_taskrating() removes taskrating, this is useful 
    //  when team task worker is changed, so remove rating from user who
    //  is task worker now
    //============================================================================
    
    function remove_taskrating() {
     global $db;
     $team_id = $this->team_id;
     $task_id = $this->task_id;
     $user_id = $this->user_id;
     
        $db->query("DELETE FROM " . table_taskrating . " WHERE  
        user_id=$user_id AND task_id=$task_id AND team_id=$team_id");
          
    }
    //============================================================================
    //  Function store() save task rating into database, if users task rating 
    //  exist it is replaced
    //============================================================================
    
    function store() {
        global $db;
    
        $user_id = $this->user_id;
        $task_id = $this->task_id;
        $team_id = $this->team_id;
        $taskrating_value = $this->taskrating_value;
        
        if ($this->user_already_voted($task_id,$team_id)){ 
             $db->query("UPDATE " . table_taskrating. "  SET taskrating_value=$taskrating_value 
                       WHERE user_id=$user_id AND task_id=$task_id AND team_id=$team_id");           
        } else { 
             $sql = "INSERT INTO " . table_taskrating. "  VALUES ($user_id, $task_id, $team_id, $taskrating_value )"; 
             
             if($db->query($sql)){   
              return true;     
              
              } else return false;       
        
        }return true;       
                     
      
       }                                
    
    //============================================================================
    //  Function user_already_voted() finds out if user already voted for this task
    //  @return true if user already voted otherwise false
    //  @param team_id,$task_id
    //============================================================================      
    
    function user_already_voted($task_id,$team_id) {
        global $db,$current_user;        
           
        $voted=$db->get_var("SELECT taskrating_value FROM ". table_taskrating. " WHERE   
                            team_id=$team_id AND task_id=$task_id AND user_id=$current_user->user_id");               
                                 
        if ($voted != null) { 
         return true; 
        }
        return false;
    }
    
  
    
      //==============================================================================
      // Function save_taskrating()  sets values  and saves values to database           
      //==============================================================================  
      
      function save_taskrating()
      {      global $current_user;
         
         $this->set_team_id($_REQUEST['id']);           
         $this->set_task_id($_POST['task_id']);                                                                            
         $this->set_user_id($current_user->user_id);                     
         $this->set_taskrating_value($_POST['taskrating_value']); 
         
          //store to database
          if (!$this->store()) { 
              //cannot save to dabase
              return false;
          }
          return true;
      }
           
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  CHECKING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     
      
    //========================================================================
    // Function check_taskrating_value checks if task rating value is correct
    // @return true if task rating value is correct otherwise false
    //========================================================================    

    function check_taskrating_value($value) {
        if ($value==null) {
            return false;
            }
         if (!ctype_digit(trim($value)) || $value > 10 || $value < 0){   
             return false;
         }    
             return true;            
    }     
    

      

 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //  SETTING FUNCTIONS
 //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   
    //============================================================================
    //  Function set_team_id() sets taskrating team_id
    //  @param team_id
    //============================================================================      
    function set_team_id($team_id) {
     $this->team_id = $team_id;
    } 
   
   //============================================================================
    //  Function set_task_id() sets taskrating task_id
    //  @param task_id
    //============================================================================      
    function set_task_id($task_id) {
     $this->task_id = $task_id;
    } 
     //============================================================================
    //  Function set_user_id() sets taskrating user_id
    //  @param user_id
    //============================================================================      
    function set_user_id($user_id) {
     $this->user_id = $user_id;
    } 
     //============================================================================
    //  Function set_taskrating_value() sets taskrating_value 
    //  @param taskrating_value
    //============================================================================      
    function set_taskrating_value($taskrating_value) {
     $this->taskrating_value = $taskrating_value;
    }
    
    //============================================================================
    //  Function get_user_tasks_rating() returns array of user tasks rating values
    //  It means ratings of user tasks (other members rated them)
    //============================================================================      
    function get_user_taskrating_values($team_id) {
        global $current_user,$db;       
             
            $rating_array =  $db->get_results("SELECT avg(taskrating_value) as average_rating FROM " . table_taskrating . 
            " JOIN " . table_tasks . " USING(task_id) WHERE task_worker_id=$current_user->user_id".
            " AND team_id=$team_id GROUP BY task_id");
        
        if ($rating_array) {   
            return $rating_array;   
            }
                        
            return false;    
    }     
  

     
    
}
?>

